package com.example.auv_660.command;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

public abstract class AUVCommand {
    public abstract void writeCommand(String command);

    public abstract void readCommand();

    protected int getAUVModuleNumber(String moduleName)
    {
        String file = "/META-INF/services/com.example.auv_660.moduleload.ModuleLoad";
        URL fileURL = this.getClass().getResource(file);
        int number = 0;
        System.out.println("fileURL: " + fileURL.getPath());
        if (fileURL != null)
        {
            try
            {
                BufferedReader bufferedReader = null;
                bufferedReader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(file)));
                String content = "";
                content = bufferedReader.readLine();
                while (content != null)
                {
                    String [] contents = content.split("\n")[0].split("\\.");
                    int length = contents.length;
                    if (moduleName.equals(contents[length - 1]))
                    {
                        return number;
                    }
                    number++;
                    content = bufferedReader.readLine();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
        return -1;
    }
}
